/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * VentanaPC.java
 *
 * Created on 05/08/2011, 17:28:19
 */
package tsb.UI.Ventanas;

import java.awt.Image;
import java.awt.Toolkit;
import java.util.logging.Level;
import java.util.logging.Logger;
import tsb.dominio.excepciones.MACException;
import tsb.dominio.interfaces.*;
import java.util.List;
import java.util.ArrayList;
import java.util.Iterator;
import javax.swing.ImageIcon;
import tsb.UI.Ventanas.VentanaDeError;
import tsb.dominio.dispositivos.Dispositivo;
import tsb.dominio.enumeraciones.AsignacionIP;
import tsb.dominio.dispositivos.hosfinal.PC;
import tsb.gestores.GestorDeDispositivos;

/**
 *
 * @author Fede
 */
public final class VentanaPC extends javax.swing.JFrame
{
    
    private PC pc;
    private ArrayList<InterfaceDeConeccion> listaInterfaces;
    private tsb.gestores.GestorDeDispositivos gestorDisp;
    private tsb.UI.PanelArrastrar.BotonPersonalizado btnPersonalizado;
    
    
    /** Creates new form VentanaPC */
    public VentanaPC(String nombreAMostrar, tsb.gestores.GestorDeDispositivos gestorDisp, Dispositivo disp,tsb.UI.PanelArrastrar.BotonPersonalizado btnPersonalizado)
    {
        
        pc=(PC) disp;
        this.gestorDisp=gestorDisp;
        initComponents();
        this.setLocationRelativeTo(null);
       
        this.setTitle(pc.getNombreAMostrar());
        
        listaInterfaces = new ArrayList<InterfaceDeConeccion>();
        this.btnPersonalizado=btnPersonalizado;
        this.setPC(pc);
        if (this.panelPC.getDhcpOStatic())
        {
            this.panelPC.deshabilitarText();
        }
        else
        {
            this.panelPC.habilitarText();
        }
       
        
        
    }
    
  @Override
  
  public Image getIconImage() {
   //Image retValue = Toolkit.getDefaultToolkit().getImage(ClassLoader.getSystemResource("/tsb/UI/Imagenes/PC_61x16.png"));
      Image retValue = Toolkit.getDefaultToolkit().getImage(getClass().getResource("/tsb/UI/Imagenes/PC_61x16.png"));
   //getClass().getResource("/tsb/UI/Imagenes/CONEXION_SERIAL_DTE_16x16.png")


   return retValue;
}
public GestorDeDispositivos getGestorDeDispositivos()
    {
     return this.gestorDisp;
    }
    
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        panelBotones = new javax.swing.JPanel();
        btnCancelar = new javax.swing.JButton();
        btnAceptar = new javax.swing.JButton();
        panelPC = new tsb.UI.Paneles.PC.PanelPC(this);

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("PC");
        setBackground(new java.awt.Color(255, 255, 255));
        setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        setIconImage(getIconImage());
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                formWindowClosing(evt);
            }
        });

        panelBotones.setBackground(new java.awt.Color(255, 255, 255));

        btnCancelar.setText("Cancelar");
        btnCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelarActionPerformed(evt);
            }
        });

        btnAceptar.setText("Aceptar");
        btnAceptar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAceptarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout panelBotonesLayout = new javax.swing.GroupLayout(panelBotones);
        panelBotones.setLayout(panelBotonesLayout);
        panelBotonesLayout.setHorizontalGroup(
            panelBotonesLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, panelBotonesLayout.createSequentialGroup()
                .addContainerGap(273, Short.MAX_VALUE)
                .addComponent(btnAceptar)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnCancelar)
                .addContainerGap())
        );
        panelBotonesLayout.setVerticalGroup(
            panelBotonesLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, panelBotonesLayout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(panelBotonesLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnCancelar)
                    .addComponent(btnAceptar))
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(panelBotones, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(panelPC, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(panelPC, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(panelBotones, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnAceptarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAceptarActionPerformed
        if(this.panelPC.getDhcpOStatic())
        {
           agregarDisp();
        }
        else
        {
           if(this.panelPC.existeGateway()==true && this.panelPC.existeDns()==true)
           {
               agregarDisp();
           }
           else
           {
                VentanaDeError.mostrarDialog("Error al configurar dispositivo", "      Ingrese Gateway y DNS     ", 0);
           }
        }
  
    }//GEN-LAST:event_btnAceptarActionPerformed
    private void agregarDisp()
    {
            
                PC pc= this.getPC();
                gestorDisp.actualizarDisp(pc);
                btnPersonalizado.setText(pc.getNombreAMostrar());
                this.btnPersonalizado.setVentanaAbierta(false);
                this.dispose();


    }
    private void btnCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelarActionPerformed
        this.btnPersonalizado.setVentanaAbierta(false);
        this.dispose();
    }//GEN-LAST:event_btnCancelarActionPerformed

    private void formWindowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosing
        this.btnPersonalizado.setVentanaAbierta(false);
    }//GEN-LAST:event_formWindowClosing
  
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAceptar;
    private javax.swing.JButton btnCancelar;
    private javax.swing.JPanel panelBotones;
    private tsb.UI.Paneles.PC.PanelPC panelPC;
    // End of variables declaration//GEN-END:variables

    public void setPC(PC pc)
    {
        this.pc=pc;
        this.panelPC.setNombreAMostrar(pc.getNombreAMostrar());
        this.panelPC.setEncendido(pc.getEstado());
        if(pc.getDHCP_Static()!=null)
        {
            this.panelPC.setAsignacionIP(pc.getDHCP_Static());
            //this.panelPC.habilitarText();
        }
      
        this.panelPC.setGateway(pc.getGateway());
        this.panelPC.setServidorDNS(pc.getServidorDNS());

        //Para pestaña interfaces
        if(pc.getInterfaces() != null)
        {
            this.panelPC.agregarInterfaces(pc.getInterfaces());
        }

    }
    
    public PC getPC()
    {
        this.pc.setNombreAMostrar(this.panelPC.getNombreAMostrar());
        this.pc.setEstado(this.panelPC.getEncendido());
        this.pc.setDHCP_Static(this.panelPC.getAsignacionIP());
        //this.pc.setEstado(this.panelPC.getEncendido());
        //this.pc.setGateway(this.panelPC.getServdorDNS());
        if (this.panelPC.getDhcpOStatic()==false)
        {
             this.pc.setGateway(this.panelPC.getGateway());
             this.pc.setServidorDNS(this.panelPC.getServdorDNS());
             this.panelPC.habilitarText();
        }
        else
        {
            this.panelPC.deshabilitarText();
        }

//        this.listaInterfaces.add(et);
//---------------------------
            this.listaInterfaces.add(this.panelPC.getInterfaz1());
      
        if(this.panelPC.getPestaña2()==true)
        {
            //this.pc.agregarInterfaz(this.panelPC.getInterfaz2());
            this.listaInterfaces.add(this.panelPC.getInterfaz2());
        }
//        this.pc.setInterfaces(listaInterfaces);
////        else
//        {
//            if(this.pc.getInterfaces().size()==2)
//            {
//                 this.pc.eliminarInterfaz();
//            }
//
//
//        }
           this.pc.setInterfaces(listaInterfaces);
        return pc;
        
    }
    private void actualizarInterfaz1(Ethernet e)
    {
        this.listaInterfaces.set(0, e);
    }
    
    
}
